package cn.csg.ams.collect2.poptopics.service.impl;

import cn.csg.ams.collect2.poptopics.dao.MkPrPfOutageBlackListDao;
import cn.csg.ams.collect2.poptopics.datahub.TupleRecordDataUtils;
import cn.csg.ams.collect2.poptopics.entity.RecordInfo;
import cn.csg.ams.collect2.poptopics.entity.ao.MkPrPfOutageBlackListAO;
import cn.csg.ams.collect2.poptopics.service.MkPrPfOutageBlackListService;
import cn.csg.ams.collect2.poptopics.infrastructure.utils.DateUtil;
import com.aliyun.datahub.client.model.TupleRecordData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: zhanggang
 * @ClassName: MkPrPfOutageBlackListServiceImpl
 * @date: 2023-12-27 10:58:49
 * @Description: 停电告警黑名单(mk_pr_pf_outage_black_list)表服务实现类
 */
@Service("mkPrPfOutageBlackListService")
public class MkPrPfOutageBlackListServiceImpl implements MkPrPfOutageBlackListService {

    @Autowired
    private MkPrPfOutageBlackListDao mkPrPfOutageBlackListDao;


    /**
     * @Title:getMkPrPfOutageBlackListTotal
     * @Description:查询总数
     * @date: 2023-12-27 10:58:49
     * @params: mkPrPfOutageBlackListAo 查询条件
     * @return：int
     * @throw：
     */
    @Override
    public int getMkPrPfOutageBlackListTotal(MkPrPfOutageBlackListAO mkPrPfOutageBlackListAo) {
        return mkPrPfOutageBlackListDao.getMkPrPfOutageBlackListTotal(mkPrPfOutageBlackListAo);
    }


    /**
     * 校验是否在调试计划内
     *
     * @param data       告警数据
     * @param recordInfo 档案信息
     * @return boolean
     */
    @Override
    public boolean checkDebugPlan(TupleRecordData data, RecordInfo recordInfo) {
        // 告警时间
        String alarmTime = TupleRecordDataUtils.getStringField(data, 23);
        Date outageStartTime = DateUtil.parse(alarmTime, "yyyyMMddHHmmss");
        // 告警来源，代码分类：GJLY=|{1终端；2电表；3主站}
        String alarmSourCode = TupleRecordDataUtils.getStringField(data, 11);
        // 运行终端标识
        String runTermId = TupleRecordDataUtils.getStringField(data, 1);

        if ("1".equals(alarmSourCode)) {
            // 查询终端是否在调试计划内
            Long count = mkPrPfOutageBlackListDao.checkTerm(outageStartTime, runTermId);
            return count > 0;
        }

        if ("2".equals(alarmSourCode)) {
            // 查询用户是否在调试计划内
            Long count = mkPrPfOutageBlackListDao.checkUser(outageStartTime, recordInfo.getElecCustNo());
            return count > 0;
        }
        return false;
    }

}
